package web;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mattpayne.jrac.people.dao.PeopleDaoExeception;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import bus.Person;
import bus.PersonManager;

public class EditPeopleFormController extends SimpleFormController {
	private PersonManager personManager;

	public ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors) {

		dumpRequestParams(request);
		// int increase = ( (PriceIncrease) command ).getPercentage();
		// personManager.increasePrice( increase );
		Person newPerson = (Person) command;
		//TODO: MP-- Isolate why this is!
		newPerson.setName(Utils.undupe(newPerson.getName()));
		newPerson.setEmail(Utils.undupe(newPerson.getEmail()));
		newPerson.setRole(Utils.undupe(newPerson.getRole()));
		newPerson.setCmd(Utils.undupe(newPerson.getCmd()));
		try {
			String cmd = (String) newPerson.getCmd();
			p("&&&&&&&\n&&&&&&&&&&\n"
					+getClass().getName()+
					".onSubmit(....) &&&&&&&&&&\nnewPerson=" + newPerson );
			if (cmd.equalsIgnoreCase("new")){
				personManager.addPerson(newPerson);
			} else if (cmd.equalsIgnoreCase("del")){
				personManager.deletePerson(newPerson);				
			} else if (
					cmd.equalsIgnoreCase("edit")
					||
					"edit,edit".equalsIgnoreCase(cmd)
					){
				personManager.savePerson(newPerson);
			}			else {
				p("Taking default since cmd='"+cmd+"'");
				personManager.savePerson(newPerson);
			}
		} catch (PeopleDaoExeception e) {
			// TODO: Handle this gracefully....
			e.printStackTrace();
		}
		return new ModelAndView(new RedirectView(getSuccessView()));
	}

	private void dumpRequestParams(HttpServletRequest request) {
		Map pm = request.getParameterMap();
		for (Iterator it=pm.keySet().iterator(); it.hasNext(); )  {
			String key = (String) it.next();
			String value = (String) request.getParameter(key);
			p("dumpRequestParams: "+key+"="+value);
		}
	}

	private void p(String msg) {
		System.out.println(getClass().getName() + "EPFC msg=" + msg);
	}

	/**
	 * IOU JavaDoc
	 */
	protected Object formBackingObject(HttpServletRequest request) {
		/* LL: formBackingObject(HttpServletRequest request, HttpServletResponse response)
		   does not get called automatically.
		*/
		Person person=null;
		String email = (String) request.getParameter("email");
		String cmd = (String) request.getParameter("cmd");

		/*
		HttpSession session = request.getSession();
		session.setAttribute("demo", "" + new java.util.Date());
		session.setAttribute("cmd", cmd);
		session.setAttribute("email", email);
		*/
		p("&&&&&&&\n&&&&&&&&&&\n&&&&&&&&&&\ncmd=" + cmd);
		if (!"new".equals(cmd)) {
		person = getByEmail(email);
	
		} else {
			person = new Person();
		}
		person.setCmd(cmd);
		return person;

	}

	private Person getByEmail(String email) {
		Person person = null;
		List lst = personManager.getPeople();
		for (Iterator it = lst.iterator(); it.hasNext();) {
			person = (Person) it.next();
			if (email.equalsIgnoreCase(person.getEmail())) {
				break;
			}
			person = null;
		}
		p("******\n*******\n****** getByEmail("+email+") returns: " + person);
		return person;
	}

	public PersonManager getPersonManager() {
		return personManager;
	}

	public void setPersonManager(PersonManager personManager) {
		this.personManager = personManager;
	}

}
